<!DOCTYPE html>
<html ang="en" class="no-js" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>留言管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--顶部搜索框-->
<form class="layui-form" action="/posts/queryAll" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">访客ID:</label>
        <div class="layui-input-inline">
            <input type="text" name="visitor_id" autocomplete="off" placeholder="来访者ID" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-normal" id="myButton">重置</button>
        </div>
    </div>
</form>
<!--用户列表显示-->
<table id="postTable" lay-filter="postFilter"></table>

<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
    </div>
</script>


<!--JavaScript代码区域-->
<script src="/layui/layui.js"></script>
<!--列表信息显示-->
<script>
    layui.use(['table', 'layer', 'form', 'jquery', 'upload'], function () {
        let table = layui.table;//数据表格模块
        let layer = layui.layer;//弹出层模块
        let form = layui.form;//表单模块
        let $ = layui.jquery;
        let upload = layui.upload;
        let $roleCheckBox = $("div[name=roles]");


        //用户列表
        let postTable = table.render({
            elem: '#postTable' //table标签的id
            , toolbar: '#toolbarHead'//开启头部工具栏，并为其绑定左侧模板
            , url: '/posts/queryMyPosts' //数据接口
            , page: true //开启分页，默认向后台发两个参数page和limit
            // , cellMinWidth: 45//自适应宽度，最短为80
            , cols: [
                [ //表头 两个中括号不能在同一行，与thymeleaf语法冲突
                    {type: 'checkbox', fixed: 'left'} //开启复选框，必须有fixed
                    , {field: 'visitorId', title: '访客ID', width: 70}
                    , {field: 'visitorName', title: '访客昵称', width: 100}
                    , {field: 'title', title: '留言标题', width: 200}
                    , {field: 'content', title: '留言内容'},
                    {fixed: 'right', title: '操作', align: 'center', toolbar: '#barRowEdit', width: 120}
                ]
            ]
        });
        //行编辑工具栏监听事件
        table.on('tool(postFilter)', function (obj) {
            //获取当前行数据
            let data = obj.data;
            //判断是删除还是编辑
            if (obj.event === 'del') {
                layer.confirm('确认删除这条留言？', function (index) {
                    //向后台发送异步请求
                    $.ajax({
                        url: "/user/deleteById",
                        type: "DELETE",
                        data: {id: data.id},
                        dataType: "json",
                        success: function (resp) {
                            if (resp.code == 0) {
                                layer.msg(resp.msg);
                                //重新加载数据
                                postTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                })
                            }
                        },
                        error: function (e) {
                            layer.msg("请求失败");
                        }
                    });
                });
            } else if (obj.event === 'edit') {

                //表单赋值，取值 这里是赋值
                form.val("userUpdateFilter", data);
                //打开弹出层
                layer.open({
                    type: 1,//指页面层
                    area: ['500px', '550px'],
                    content: $('#userUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });

            }
        });
        //头部工具栏监听事件
        table.on('toolbar(postFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            let checkData = checkStatus.data;
            switch (obj.event) {
                case 'delete':
                    //判断是否选中行
                    if (checkData.length == 0) {
                        layer.msg('请至少选择一行数据');
                        return;
                    }
                    layer.confirm('确认删除这条留言？', function (index) {
                        //获取所有选中行id
                        let ids = "";
                        for (let i = 0; i < checkData.length; i++) {
                            ids += checkData[i].id + ",";
                        }
                        ;
                        ids = ids.substr(0, ids.length - 1);
                        layer.msg(ids);
                        //调用批量删除
                        $.ajax({
                            url: "/user/deleteByIds",
                            type: "DELETE",
                            data: {ids: ids},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    postTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
            }
            ;
        });
        
        //顶部搜索框监听事件
        form.on('submit(searchFilter)', function (data) {
            //执行重载
            postTable.reload({
                where: {
                    id: data.field.id,
                    visitorId: data.field.visitorId,
                    userId: data.field.userId
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            postTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    id: ""
                    , visitorId: ""
                    , userId: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
</body>
</html>